package com.cskaoyan.bean.promote.utils;

import com.cskaoyan.bean.promote.exception.PromoteParamException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

/**
 * @author liXiaoFei
 * @date 2021/10/19 9:22
 */
public class PromoteValidationUtil {
    public static void valid(BindingResult bindingResult) throws PromoteParamException {
        if (bindingResult.hasFieldErrors()){
            FieldError fieldError = bindingResult.getFieldError();
            String field = fieldError.getField();
            Object rejectedValue = fieldError.getRejectedValue();
            String defaultMessage = fieldError.getDefaultMessage();
            String message = "请求参数：" + field + "--因为传入了：" + rejectedValue + "--没有通过校验，原因是：" + defaultMessage;
            throw new PromoteParamException(message);
        }
    }

}
